﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace QuanlyShop
{
    public partial class FrmKhachHang : Form
    {
        private OleDbConnection connect;
        private OleDbDataAdapter adapter;
        private DataSet data;
        private OleDbCommand cmd;
        // lay thong tin
        private string makh,tenkh, sdt, diachi;
        public FrmKhachHang()
        {
            InitializeComponent();
            //ketnoi("database.mdb");
            //connect.Open();
        }
        private void ketnoi(string file)
        {
            string conn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Application.StartupPath + "\\" + file;
            connect = new OleDbConnection(conn);
        }
        private void FrmKhachHang_Load(object sender, EventArgs e)
        {
            ketnoi("database.mdb");
            dataGridView1.DataSource=laykhachhang();
            xuatdata();
        }
        private void laydulieu()
        {
            tenkh = txttenkh.Text;
            sdt = txtsdt.Text;
            diachi = txtdiachi.Text;
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            laydulieu();
            connect.Open();
            string them = "";
            if (dataGridView1.Rows.Count == 0)
            {
                 them = "Insert into KHACH_HANG Values('"+0+"','" + tenkh + "','" + sdt + "','" + diachi + "')";
            }
            else
            {
                 them = "Insert into KHACH_HANG Values('" + (Int32.Parse(dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0].Value.ToString()) + Int32.Parse("1")) + "','" + tenkh + "','" + sdt + "','" + diachi + "')";
            }           
            cmd = new OleDbCommand(them,connect);
            cmd.ExecuteNonQuery();           
            MessageBox.Show("Cap nhat thanh cong", "Thong bao",MessageBoxButtons.OK, MessageBoxIcon.Information);
            dataGridView1.DataSource = laykhachhang();
            connect.Close();
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            laydulieu();
            connect.Open();
            string xoa = "Delete from KHACH_HANG where KH_ID='"+makh+"'";
            cmd = new OleDbCommand(xoa,connect);
            cmd.ExecuteNonQuery();
            connect.Close();
            MessageBox.Show("Xóa dữ liệu khách hàng thành công","Thông báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            dataGridView1.DataSource = laykhachhang();
        }
        private DataTable laykhachhang()
        {
            adapter = new OleDbDataAdapter("Select* from KHACH_HANG",connect);
            data = new DataSet();
            adapter.Fill(data);
            return data.Tables[0];
        }
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            makh = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["KH_ID"].Value.ToString();
            txttenkh.Text=dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["KH_TEN"].Value.ToString();
            txtsdt.Text=dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["KH_SDT"].Value.ToString();
            txtdiachi.Text=dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["KH_DIACHI"].Value.ToString();
        }
        private void xuatdata()
        {
            dataGridView1.Columns[0].HeaderText = "KH_ID";
            dataGridView1.Columns[0].Width = 70;
            dataGridView1.Columns[1].HeaderText="KH_TEN";
            dataGridView1.Columns[1].Width = 70;
            dataGridView1.Columns[2].HeaderText="KH_SDT";
            dataGridView1.Columns[2].Width = 70;
            dataGridView1.Columns[3].HeaderText="KH_DIACHI";
            dataGridView1.Columns[3].Width = 70;
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection rows = dataGridView1.SelectedRows;
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            laydulieu();
            connect.Open();
            string sua = "update KHACH_HANG set KH_TEN='" + txttenkh.Text + "',KH_SDT='" + txtsdt.Text + "',KH_DIACHI='" + txtdiachi.Text + "' where KH_ID ='" + makh + "'";
            cmd = new OleDbCommand(sua, connect);
            cmd.ExecuteNonQuery();
            connect.Close();
            MessageBox.Show("Sửa dữ liệu khách hàng thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            dataGridView1.DataSource = laykhachhang();
        }
      
    }
}
